#import "Controller.h"
#import "PolygonShape.h"
@class PolygonView;

@implementation Controller

- (void)updateInterface {
	decreaseButton.enabled = (polygon.numberOfSides > polygon.minimumNumberOfSides);
	increaseButton.enabled = (polygon.numberOfSides < polygon.maximumNumberOfSides);
	
	numberOfSidesLabel.text = [[NSNumber numberWithInt:[polygon numberOfSides]] stringValue];
	nameLabel.text = polygon.name;
	slider.value = polygon.numberOfSides;
	[polygonView setNeedsDisplay];
}

- (IBAction)sliderChanged {
	[self changeNumberOfSides:slider.value];
	[self updateInterface];
}

- (void)changeNumberOfSides:(int)sides {
	[polygon setNumberOfSIdes:sides];
	[defaults setInteger:polygon.numberOfSides forKey:@"numberOfSides"];
}

- (IBAction)decrease {
	[self changeNumberOfSides:([numberOfSidesLabel.text integerValue] -1)];
	[self updateInterface];
}

- (IBAction)increase {
	[self changeNumberOfSides:([numberOfSidesLabel.text integerValue] +1)];
	[self updateInterface];
}

- (void)awakeFromNib {
	defaults = [NSUserDefaults standardUserDefaults];
	if (![defaults integerForKey:@"numberOfSides"]) {
		NSLog(@"no defaults");
		[defaults setInteger:5 forKey:@"numberOfSides"];
	}
	[polygon setNumberOfSIdes:[defaults integerForKey:@"numberOfSides"]];
	nameLabel.text = polygon.name;
	[self updateInterface];
}

@end
